# -*- coding: utf-8 -*-

# Author: 一根鱼骨棒
# Date: 2020-09-18 10:55:56
# LastEditTime: 2022-01-18 09:46:32
# LastEditors: 一根鱼骨棒
# Description: 
# FilePath: \3qjwe:\Code\python\script\newspider\wuzhi_post.py
# Software: VScode
# Copyright 2020 迷彩视界

import json
import os
import time
import requests

# 时间
year = time.strftime("%Y", time.localtime())
month = time.strftime("%m", time.localtime())
today = time.strftime("%d", time.localtime())
hours = time.strftime("%H", time.localtime())
date= str(year)+str(month)+str(today)
# 下载的目录
download_path = os.getcwd()+'/news/'+date+hours+"spider/"
download_asset_path=download_path+'asset/'

POST_URL='http://localhost/index.php?f=article&v=getDataOfJson'
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36",
    "Content-Type": "application/json; charset=utf-8",
}
##########################################################
# with open(download_path+'data.json','rb') as target:
#     data=target.read()
# print(type(data))
# headers['Content-Length']=str(len(json.loads(data)))
# print(headers['Content-Length'])
# r=requests.post(data=data,url=POST_URL,headers=headers)
# print(r.text)
##########################################################
# 读取数据并逐条保存到数据库
try:
    files=os.listdir((download_path))
    print(files)
except:
    print("系统找不到指定的路径")
    os._exit(1)
total=0
for eachfile in files:
    print(eachfile)
    if "json" in eachfile:
        try:
            print(download_path+eachfile)
            with open(download_path+eachfile,'rb') as target:
                print("{}开始导入".format(eachfile))
                datas=target.read()
        except:
            print("没有目录")
            os._exit(1)
        datas = json.loads(datas)
        count=0
        succeed=0
        failed=0
        failed_list=[]
        for each in datas:
            # {byte} eachdata 
            eachdata = json.loads(datas[each])
            headers['Content-Length']=str(len(eachdata))
            r=requests.post(data=json.dumps(eachdata),url=POST_URL,headers=headers)
            info=json.loads(r.text)
            if info['code']==200:
                count=count+1
                succeed=succeed+1
                print("第{}篇上传成功".format(count))
            if info['code']==400:
                print("参数异常:{}".format(eachdata['title']))
                count=count+1
                failed=failed+1
            if info['code']==402:
                print("重复内容:{}".format(info['data']))
        
